package com.yyfax.common.form;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yyfax.common.CLog;
import com.yyfax.common.R;
import com.yyfax.common.tools.DensityUtil;

/**
 * Created by fuqiang on 2017/5/4.
 * 选择文本表单
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class FormSelectView extends LinearLayout {
    private static final String TAG = "FormSelectView";
    /**
     * 右边imagebutton的点击事件(此处用于定位输入框)
     */
    private onDrawableClickListener mDrawableListener;

    public ImageButton mRightIb;

    public TextView mRTextView;

    public FormSelectView(Context context) {
        super(context);
    }

    public FormSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public FormSelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FormRow);
//        mRightDrawable = ta.getDrawable(R.styleable.FormRow_android_drawableRight);//设置右边的图标
//        if (mRightDrawable != null) {//设置drawableRight
//            mRightDrawable.setBounds(0, 0, mRightDrawable.getMinimumWidth(), mRightDrawable.getMinimumHeight());
//            setCompoundDrawables(null, null, mRightDrawable, null);
//        }
        setGravity(Gravity.CENTER_VERTICAL);
        setPadding(0, getPaddingTop(), getPaddingRight(), getPaddingBottom());
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.select_layout, this);
        mRTextView = (TextView) view.findViewById(R.id.text_view);
        mRTextView.setEllipsize(TextUtils.TruncateAt.END);
        mRightIb = (ImageButton) view.findViewById(R.id.right_ib);
        mRightIb.setImageDrawable(ta.getDrawable(R.styleable.FormRow_drawableRight));
        mRTextView.setHint(ta.getString(R.styleable.FormRow_hint));//设置hintText
        mRTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ta.getDimension(R.styleable.FormRow_textSize, DensityUtil.sp2px(getContext(), 15)));//设置textsize

        ColorStateList rightTextColorHint = ta.getColorStateList(R.styleable.FormRow_textColorHint);//设置textcolorhint
        if (rightTextColorHint != null)
            mRTextView.setHintTextColor(rightTextColorHint);
        else
            mRTextView.setHintTextColor(getResources().getColor(R.color.color_cccccc));

        ColorStateList rightTextColor = ta.getColorStateList(R.styleable.FormRow_rightTextColor);//设置textcolor
        if (rightTextColor != null)
            mRTextView.setTextColor(rightTextColor);
        else
            mRTextView.setTextColor(getResources().getColor(R.color.color_000000));

        mRightIb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDrawableListener != null){
                    mDrawableListener.onDrawableClick(v);
                }
            }
        });

        mRTextView.setFreezesText(ta.getBoolean(R.styleable.FormRow_freezesText, true));
        mRTextView.setSaveEnabled(ta.getBoolean(R.styleable.FormRow_saveEnabled, true));
    }

    /**
     * 点击定位按钮回调
     */
    public interface onDrawableClickListener {
        void onDrawableClick(View view);
    }

    public void setonDrawableClickListener(onDrawableClickListener listener) {
        mDrawableListener = listener;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        if(event.getAction() == MotionEvent.ACTION_UP)
//            CLog.i(TAG, "MotionEvent.ACTION_UP");
////        if (event.getAction() == MotionEvent.ACTION_UP && mRightDrawable != null && (event.getRawX() >= (getRight() - mRightDrawable.getBounds().width() - getPaddingRight())) && mDrawableListener != null) {
////            mDrawableListener.onDrawableClick(this);
////            //return true;
////        }
////
////        return true;
//        if (getCompoundDrawables()[2] != null && event.getAction() == MotionEvent.ACTION_UP
//                && event.getX() > getWidth() - getTotalPaddingRight()
//                && event.getX() < getWidth() - getPaddingRight()) {
//                mDrawableListener.onDrawableClick(this);
//        }else if(mDrawableListener != null && event.getAction() == MotionEvent.ACTION_DOWN
//                && event.getX() > getWidth() - getTotalPaddingRight()
//                && event.getX() < getWidth() - getPaddingRight()){
//            return true;
//        }
//        return super.onTouchEvent(event);
//    }

    public String getFormText() {
        return mRTextView.getText().toString().trim();
    }

    public void setText(CharSequence text) {
        mRTextView.setText(text);
    }
}
